package com.wanli.agent.homepage;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ethanhua.skeleton.Skeleton;
import com.ethanhua.skeleton.SkeletonScreen;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.wanli.agent.R;
import com.wanli.agent.base.BaseActivity;
import com.wanli.agent.base.DataCallBack;
import com.wanli.agent.bean.TransferAgentNewBean;
import com.wanli.agent.bean.UserInfoBean;
import com.wanli.agent.homepage.adapter.TransferAgentListAdapter;
import com.wanli.agent.homepage.model.HomePageModelImpl;
import com.wanli.agent.homepage.model.IHomePageModel;
import com.wanli.agent.utils.SPManager;
import com.wanli.agent.widget.AppTitleBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 已划拨 服务商列表
 */
public class TransferAgentListActivity extends BaseActivity implements OnRefreshLoadMoreListener {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private IHomePageModel homePageModel;
    private List<TransferAgentNewBean.DataBean> beanList;
    private TransferAgentListAdapter listAdapter;
    private int page = 1;
    private SkeletonScreen skeletonScreen;
    private String type;
    private String child_agent_id;
    private String agent_name = "";
    private String agent_id = "";
    private String titlePrefix = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transfer_agent_list);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    private void initView() {
        refreshLayout.setRefreshHeader(new ClassicsHeader(mActivity));//设置Header
        refreshLayout.setRefreshFooter(new ClassicsFooter(mActivity));//设置Footer
        refreshLayout.setOnRefreshLoadMoreListener(this);
        type = getIntent().getStringExtra("type");
        child_agent_id = getIntent().getStringExtra("child_agent_id");
        agent_name = getIntent().getStringExtra("agent_name");
        if (!TextUtils.isEmpty(agent_name)) {
            titlePrefix = agent_name + "_";
            if (agent_name.length() > 6) {
                titlePrefix = agent_name.substring(0, 6) + "_";
            }
        } else {
            titlePrefix = "";
        }
        titleBar.setTitle(titlePrefix + "已划拨服务商");
        UserInfoBean userInfoBean = new Gson().fromJson(SPManager.getInstance().getUserInfoJson(), UserInfoBean.class);
        if (userInfoBean != null) {
            agent_id = userInfoBean.getData().getId() + "";
        }
    }

    private void initData() {
        homePageModel = new HomePageModelImpl();
        beanList = new ArrayList<>();
        listAdapter = new TransferAgentListAdapter(beanList, type);
        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        recyclerView.setAdapter(listAdapter);

        skeletonScreen = Skeleton.bind(recyclerView)
                .adapter(listAdapter)
                .color(R.color.shimmer_color_1)
                .shimmer(true)
                .angle(20)
                .frozen(false)
                .duration(1000)
                .count(20)
                .load(R.layout.item_skeleton_machines_list)
                .show();

        requestData();
    }


    public void requestData() {
        homePageModel.requestChildDeviceNotBelong(page, child_agent_id, type, new DataCallBack<TransferAgentNewBean>() {
            @Override
            public void onSuccessful(TransferAgentNewBean bean) {
                if (bean.getData() != null && bean.getData().size() > 0) {
                    beanList.addAll(bean.getData());
                    int totalNum = 0;
                    StringBuffer buffer = new StringBuffer();
                    for (TransferAgentNewBean.DataBean agentNewBean : bean.getData()) {
                        buffer.append(agentNewBean.getId() + "_");
                        totalNum = totalNum + agentNewBean.getTotal_count();
                    }
                    titleBar.setTitle(titlePrefix + "已划拨服务商(" + totalNum + ")");
                    if (agent_id.equals(child_agent_id)) {
                        if (buffer.length() > 0) {
                            SPManager.getInstance().saveChildAgentId(buffer.substring(0, buffer.length() - 1));
                        }
                    }
                    if (page == 1) {
                        refreshLayout.finishRefresh();
                    } else {
                        refreshLayout.finishLoadMore();
                    }
                } else {
                    if (page != 1) {
                        refreshLayout.finishLoadMoreWithNoMoreData();
                    } else {
                        refreshLayout.finishRefresh();
                    }
                }
                if (beanList.size() > 0) {
                    llNoData.setVisibility(View.GONE);
                } else {
                    llNoData.setVisibility(View.VISIBLE);
                }
                listAdapter.notifyDataSetChanged();
                if (page == 1) {
                    skeletonScreen.hide();
                }
            }

            @Override
            public void onFailed(String msg, String code) {
                if (page == 1) {
                    skeletonScreen.hide();
                }
            }
        });
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page++;
        requestData();
        refreshLayout.finishLoadMore(30000);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refreshData();
    }

    public void refreshData() {
        page = 1;
        beanList.clear();
        requestData();
        refreshLayout.finishRefresh(30000);
    }


}
